﻿/********************************************************************
	日期:2022/4/28 21:17
	名称:NetSvc
	邮箱:yunnibbd@163.com
	作者:yunni
	描述:网络服务
*********************************************************************/

using System;
using System.Collections.Generic;
using System.Threading;
using PENet;
using PEProtocol;

public class MsgPack{
    public ServerSession session;
    public GameMsg msg;

    public MsgPack(ServerSession session, GameMsg msg){
        this.session = session;
        this.msg = msg;
    }
}

public class NetSvc{
    private static NetSvc instance = null;

    public static NetSvc Instance{
        get{
            if (instance == null){
                instance = new NetSvc();
            }

            return instance;
        }
    }

    public static readonly string obj = "lock";
    private Queue<MsgPack> msgPackQue = new Queue<MsgPack>();

    public void Init(){
        PESocket<ServerSession, GameMsg> server = new PESocket<ServerSession, GameMsg>();
        PECommon.Log("NetSvc StartAsServer");
        server.StartAsServer(SvcCfg.svcIP, SvcCfg.svcPort);
        PECommon.Log("NetSvc Init done");
    }

    public void AddMsgQue(ServerSession serverSession, GameMsg msg){
        lock (obj){
            msgPackQue.Enqueue(new MsgPack(serverSession, msg));
        }
    }

    public void Update(){
        if (msgPackQue.Count == 0){
            Thread.Sleep(50);
            return;
        }

        //PECommon.Log("PackCount:" + msgPackQue.Count);
        MsgPack pkt;
        lock (obj){
            pkt = msgPackQue.Dequeue();
        }

        HandOutMsg(pkt);
    }

    private void HandOutMsg(MsgPack pkt){
        switch ((CMD) pkt.msg.cmd){
            case CMD.ReqLogin:
                LoginSys.Instance.ReqLogin(pkt);
                break;
            case CMD.ReqRename:
                LoginSys.Instance.ReqRename(pkt);
                break;
            case CMD.ReqGuide:
                GuideSys.Instance.ReqGuide(pkt);
                break;
            case CMD.ReqStrong:
                StrongSys.Instance.ReqStrong(pkt);
                break;
            case CMD.SndChat:
                ChatSys.Instance.SndChat(pkt);
                break;
            case CMD.ReqBuy:
                BuySys.Instance.ReqBuy(pkt);
                break;
            case CMD.ReqHeart:
                ServerRoot.ReqHeart(pkt);
                break;
            case CMD.ReqTakeTaskReward:
                TaskSys.Instance.TakeTaskReward(pkt);
                break;
            case CMD.ReqFBFight:
                FuBenSys.Instance.ReqFBFight(pkt);
                break;
        }
    }
}